//
//  DLInfoBgView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/26.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLInfoBgView.h"
#import "DLInfoListModel.h"
#import "DLInfoTC.h"
#import "DLShortInfoTC.h"

@interface DLInfoBgView()

@property (nonatomic, assign) NSInteger pageNum;

@end



@implementation DLInfoBgView


- (instancetype) initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.pageNum = 1;
        
        UITableView *infoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) style:UITableViewStylePlain];
        infoTableView.delegate = self;
        infoTableView.dataSource = self;
        infoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:infoTableView];
        self.infoTableView = infoTableView;
        
        [infoTableView registerClass:[DLInfoTC class] forCellReuseIdentifier:@"DLInfoTC"];
        [infoTableView registerClass:[DLShortInfoTC class] forCellReuseIdentifier:@"DLShortInfoTC"];
        
        infoTableView.estimatedRowHeight = 0;
        infoTableView.estimatedSectionHeaderHeight = 0;
        infoTableView.estimatedSectionFooterHeight = 0;
        
        infoTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            self.pageNum=1;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DLReloadNewInfoData" object:[NSString stringWithFormat:@"%ld", self.pageNum]];
        }];
        
        infoTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.pageNum++;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DLLoadMoreInfoData" object:[NSString stringWithFormat:@"%ld", self.pageNum]];
        }];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endAllRefresh) name:@"DLInfoTableViewEndRefreshing" object:nil];
        
    }
    return self;
}
- (void) endAllRefresh {
    
    [self.infoTableView.mj_header endRefreshing];
    [self.infoTableView.mj_footer endRefreshing];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
//    NSLog(@"分区数：%ld", self.infoViewDataSource.count);
    return self.infoViewDataSource.count;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DLInfoListModel *model = self.infoViewDataSource[indexPath.row];
    if (model.fatherClassify.integerValue == 99) {
        
        return [DLShortInfoTC getCellHeightWithModel:model];
    }
    
    return 120;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DLInfoTC *cell = [tableView dequeueReusableCellWithIdentifier:@"DLInfoTC"];
    DLShortInfoTC *shortCell = [tableView dequeueReusableCellWithIdentifier:@"DLShortInfoTC"];
    
    DLInfoListModel *model = self.infoViewDataSource[indexPath.row];
    
    if ([model.fatherClassify isEqualToString:@"99"]) {
        shortCell.shortInfoModel = model;
        return shortCell;
    } else {
        cell.infoModel = model;
        return cell;
    }
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DLInfoListModel *model = self.infoViewDataSource[indexPath.row];
    model.isNewsRead = YES;
    
    if ([model.fatherClassify isEqualToString:@"99"]) {
        model.isZhiXunShowAll = !model.isZhiXunShowAll;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.infoTableView reloadData];
        });
    } else {
        
        [self.infoTableView reloadData];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            DLNormalWebVC *bvc = [DLNormalWebVC new];
            bvc.urlString = HTTPInfoDetail;
            bvc.newsId = model.ID;
            bvc.titleString = @"详情";
            bvc.webViewType = DLWebViewType_InfoDetail;
            [kuserManager.lastVc.navigationController pushViewController:bvc animated:YES];
        });
    }
    
}

- (void) setInfoViewDataSource:(NSArray *)infoViewDataSource {
    _infoViewDataSource = infoViewDataSource;
    NSLog(@"infoView-文章数量是：%ld", infoViewDataSource.count);
    [self.infoTableView reloadData];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
